import Vue from 'vue'
import Vuex from 'vuex'
import createPersisted from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        lists: [
            {
                state: 0,
                title: '任务1'
            },
            {
                state: 1,
                title: '任务2'
            },
        ],
    },
    mutations: {
        add(state, obj) {
            state.lists.push(obj)
        },
        change(state, i) {
            state.lists.forEach((item, index) => {
                if (index == i) {
                    if (item.state == 0) {
                        item.state = 1
                    } else {
                        item.state = 0
                    }
                }
            })
        },
        del(state, i) {
            state.lists.forEach((item, index) => {
                if (index == i) {
                    state.lists.splice(index, 1)
                }
            })
        }
    },
    getters: {
        len1(state) {
            let list1 = []
            state.lists.forEach((item, i, arr) => {
                if (item.state == '0') {
                    list1.push(item)
                }
            })
        },
        len2(state) {
            let list2 = []
            state.lists.forEach((item, i, arr) => {
                if (item.state == '1') {
                    list2.push(item)
                }
            })
        }
    },
    actions: {},
    modules: {},
    plugins: [createPersisted()],
})
